package com.example.travelsn.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

/**
 * @author imotto
 * @version 1.0
 * a means for the use of SQLite
 */
public class DBOpenHelper extends SQLiteOpenHelper {
    public static final String CREATE_USER = "create table user ("
            //primary key设置为主键，autoincrement关键字表示该列是自增长的
            + "userid integer primary key autoincrement,"
            + "username text,"
            + "password text,"
            + "type text)";
    public static final String CREATE_SELLER = "create table seller ("+
            "sellerid integer primary key autoincrement,"
            + "sellername text,"
            + "password text,"
            + "type text,"
            + "latitude real,"
            + "longitude real)";
    public static final String CREATE_FEEDBACK = "create table feedback ("+
            " feedid integer primary key autoincrement,"
            +"username text,"
            +"status text,"
            + "content text)";
    public static final String CREATE_VIEWSPOT = "create table viewspot ("+
            "viewid integer primary key autoincrement,"
            + "viewname text,"
            + "viewtype text)";
    public static final String CREATE_USERTYPE = "create table usertype ("+
            "usertypeid integer primary key autoincrement,"
            + "usertypename text)";
    public static final String CREATE_GOODSTYPE = "create table goodstype ("+
            "goodstypeid integer primary key autoincrement,"
            + "goodstypename text)";
    public static final String CREATE_GOODS = "create table goods ("+
            "goodsid integer primary key autoincrement,"
            + "sellername text,"
            + "unitprice real,"
            + "goodstype text,"
            + "goodsname text)";

    public DBOpenHelper(Context context,String name) {
        super(context, name, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //user table
        db.execSQL(CREATE_USER);
        db.execSQL(CREATE_SELLER);
        db.execSQL(CREATE_GOODS);
        db.execSQL(CREATE_VIEWSPOT);
        db.execSQL(CREATE_FEEDBACK);
        db.execSQL(CREATE_GOODSTYPE);
        db.execSQL(CREATE_USERTYPE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists user");
        db.execSQL("drop table if exists  seller");
        db.execSQL("drop table if exists viewspot");
        db.execSQL("drop table if exists  goods");
        db.execSQL("drop table if exists feedback");
        onCreate(db);
    }
}
